#define _CRT_SECURE_NO_WARNINGS 1
#include "SepLine.h"

void SLinit(SL* sl)
{
	sl->arr = NULL;
	sl->size = sl->capacity = 0;
}

void SLdestory(SL* sl)
{
	free(sl->arr);
	sl->arr = NULL;
	sl->size = sl->capacity = 0;
}

void SLapply(SL * sl)
{
	if (sl->size == sl->capacity)
	{
		sl->capacity = sl->capacity == 0 ? 4 : 2 * sl->capacity;
		SLtype* tmp = (SLtype*)realloc(sl->arr, sl->capacity * sizeof(SLtype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(1);
		}
		sl->arr = tmp;
		tmp = NULL;
	}
}
void SL_in_back(SL* sl, SLtype x)
{
	assert(sl);
	SLapply(sl);
	sl->arr[sl->size++] = x;
}

void SL_in_front(SL* sl, SLtype x)
{
	assert(sl);
	SLapply(sl);
	for (int i = sl->size; i > 0; i--)
	{
		sl->arr[i] = sl->arr[i - 1];
	}
	sl->arr[0] = x;
	sl->size++;
}

void SL_out_back(SL* sl)
{
	assert(sl);
	assert(sl->size);
	sl->size--;
}

void SLoutput(SL* sl)
{
	for (int i = 0; i < sl->size; i++)
		printf("%d ", sl->arr[i]);
	printf("\n");
}

void SL_out_front(SL* sl)
{
	assert(sl);
	assert(sl->size);
	for (int i = 0; i < sl->size - 1; i++)
		sl->arr[i] = sl->arr[i + 1];
	sl->size--;
}